package net.wexoo.group_cost_calculator.gui;

import java.util.ArrayList;
import java.util.Date;

import net.wexoo.group_cost_calculator.R;
import net.wexoo.group_cost_calculator.dao.DBHelper;
import net.wexoo.group_cost_calculator.enitities.Calculation;
import net.wexoo.group_cost_calculator.enitities.Person;
import net.wexoo.group_cost_calculator.gui.util.UIUtils;
import net.wexoo.group_cost_calculator.proxy.ProxyFactory;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

public class HomeActivity extends OrmLiteBaseActivity<DBHelper> {

   private Calculation selectedCalculation = null;
   private Integer numberOfPersons;
   private boolean setNumberOfPersonEditTextToMemberSize;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.home);

      ProxyFactory.getProxy(this).setAllCalculations();

      if (!ProxyFactory.getProxy(this).getCaluculations().isEmpty()) {
         ProxyFactory.getProxy(this).setSelectedCalculation(ProxyFactory.getProxy(this).getCaluculations().get(0));
         setNumberOfPersonEditTextToMemberSize = true;
      } else {
         ((EditText) findViewById(R.id.et_number_of_persons)).setText("3");
      }
   }

   @Override
   protected void onResume() {

      if (ProxyFactory.getProxy(this).getSelectedCalculation() != null) {
         selectedCalculation = ProxyFactory.getProxy(this).getSelectedCalculation();

         ((EditText) findViewById(R.id.et_calculation_title)).setText(selectedCalculation.getTitle());

         if (setNumberOfPersonEditTextToMemberSize) {
            ((EditText) findViewById(R.id.et_number_of_persons)).setText(String.valueOf(selectedCalculation
                  .getMembers().size()));
            setNumberOfPersonEditTextToMemberSize = false;
         }
      }

      if (!((EditText) findViewById(R.id.et_number_of_persons)).getText().equals("")) {
         numberOfPersons = Integer.valueOf(((EditText) findViewById(R.id.et_number_of_persons)).getText()
               .toString());

         if (numberOfPersons > 0) {
            LinearLayout llHomeLayout = (LinearLayout) findViewById(R.id.ll_home_layout);
            llHomeLayout.removeAllViews();
            LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            for (int i = 0; i < numberOfPersons; i++) {
               View myView = linflater.inflate(R.layout.cost_position, null);

               // Set an id to access values later on
               EditText personNameEdit = (EditText) myView.findViewById(R.id.et_person_name);
               personNameEdit.setId(i + 1000);
               if (selectedCalculation != null && selectedCalculation.getMembers().size() >= numberOfPersons) {
                  personNameEdit.setText(selectedCalculation.getMembers().get(i).getName());
               } else {
                  personNameEdit.setText("Person" + (i + 1));
               }

               EditText personCostEdit = (EditText) myView.findViewById(R.id.et_person_costs);
               personCostEdit.setId(i);
               if (selectedCalculation != null && selectedCalculation.getMembers().size() >= numberOfPersons) {
                  personCostEdit.setText(selectedCalculation.getMembers().get(i).getPayedCosts().toString());
               } else {
                  personCostEdit.setText("0.0");
               }

               llHomeLayout.addView(myView);
            }
         } else {
            UIUtils.showLongToast(this, R.string.toast_enter_value_greater_than_zero);
         }
      } else {
         UIUtils.showLongToast(this, R.string.toast_enter_value);
      }
      super.onResume();
   }

   public void updateView(View v) {
      switch (v.getId()){
         case R.id.b_update_view:
            setNewSelectedCalculation();
            onResume();
            break;
         case R.id.b_calculate_results:
            setNewSelectedCalculation();

            startActivity(new Intent(this, ResultActivity.class));
            break;
      }
   }

   private void setNewSelectedCalculation() {
      if (selectedCalculation == null) {
         selectedCalculation =
               new Calculation(
                     ((EditText) findViewById(R.id.et_calculation_title)).getText().toString(),
                     getPersonData(),
                     new Date());
      }
      ProxyFactory.getProxy(this).setSelectedCalculation(selectedCalculation);
   }

   private ArrayList<Person> getPersonData() {
      ArrayList<Person> persons = new ArrayList<Person>();

      for (int i = 0; i < numberOfPersons; i++) {
         persons.add(new Person(
               ((EditText) findViewById(i + 1000)).getText().toString(),
               Double.valueOf(((EditText) findViewById(i)).getText().toString())));
      }
      return persons;
   }
}